<!--
  提现审核对话框组件
  @description 提现申请审核功能的对话框组件
  @author AI Assistant
  @since 1.0.0
-->
<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="700px">
    <div v-loading="loading">
      <!-- 提现信息展示 -->
      <el-descriptions :column="2" border class="mb-20px" v-if="withdrawData">
        <el-descriptions-item :label="t('settle.withdraw.withdrawNo')">
          {{ withdrawData.withdrawNo }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.withdraw.merchantName')">
          {{ withdrawData.merchantName }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.withdraw.withdrawAmount')">
          <span class="amount-text">¥{{ (withdrawData.withdrawAmount / 100).toFixed(2) }}</span>
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.withdraw.feeAmount')">
          <span class="fee-text">¥{{ (withdrawData.feeAmount / 100).toFixed(2) }}</span>
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.withdraw.actualAmount')">
          <span class="actual-text">¥{{ (withdrawData.actualAmount / 100).toFixed(2) }}</span>
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.withdraw.bankName')">
          {{ withdrawData.bankName }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.withdraw.bankAccount')">
          {{ withdrawData.bankAccount }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.withdraw.accountName')">
          {{ withdrawData.accountName }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.withdraw.applyTime')" :span="2">
          {{ formatDate(withdrawData.applyTime) }}
        </el-descriptions-item>
      </el-descriptions>

      <!-- 审核表单 -->
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px">
        <el-form-item :label="t('settle.withdraw.auditResult')" prop="auditResult">
          <el-radio-group v-model="formData.auditResult" :disabled="true">
            <el-radio :label="WithdrawStatusEnum.APPROVED" v-if="auditType === 'approve'">
              <el-tag type="success">{{ t('settle.withdraw.auditApprove') }}</el-tag>
            </el-radio>
            <el-radio :label="WithdrawStatusEnum.REJECTED" v-if="auditType === 'reject'">
              <el-tag type="danger">{{ t('settle.withdraw.auditReject') }}</el-tag>
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item :label="t('settle.withdraw.auditRemark')" prop="auditRemark">
          <el-input
            v-model="formData.auditRemark"
            type="textarea"
            :placeholder="t('settle.common.pleaseInput') + t('settle.withdraw.auditRemark')"
            :rows="4"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>

        <!-- 审核通过时的额外字段 -->
        <template v-if="auditType === 'approve'">
          <el-form-item :label="t('settle.withdraw.expectedTime')" prop="expectedTime">
            <el-date-picker
              v-model="formData.expectedTime"
              type="datetime"
              :placeholder="t('settle.withdraw.rules.expectedTimeRequired')"
              value-format="YYYY-MM-DD HH:mm:ss"
              style="width: 100%"
              :disabled-date="disabledDate"
            />
            <div class="form-tip">
              {{ t('settle.withdraw.expectedTimeTip') }}
            </div>
          </el-form-item>

          <el-form-item :label="t('settle.withdraw.priority')" prop="priority">
            <el-select
              v-model="formData.priority"
              :placeholder="t('settle.withdraw.rules.priorityRequired')"
              style="width: 100%"
            >
              <el-option
                v-for="(label, value) in priorityOptions"
                :key="value"
                :label="label"
                :value="parseInt(value)"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-form>
    </div>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="loading">
        {{ t('settle.common.confirm') }}
      </el-button>
      <el-button @click="dialogVisible = false">
        {{ t('settle.common.cancel') }}
      </el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { useI18n } from 'vue-i18n'
import { formatDate } from '@/utils/formatTime'
import { auditWithdrawApplyApi } from '@/api/settle/withdraw'
import type { WithdrawVO, AuditWithdrawReqVO } from '@/api/settle/types'
import { WithdrawStatusEnum, PriorityEnum } from '@/api/settle/types'
import { Dialog } from '@/components/Dialog'

defineOptions({ name: 'WithdrawAuditDialog' })

const { t } = useI18n()

// 弹窗相关
const dialogVisible = ref(false)
const loading = ref(false)
const auditType = ref<'approve' | 'reject'>('approve')

// 提现数据
const withdrawData = ref<WithdrawVO>()

// 表单相关
const formRef = ref<FormInstance>()
const formData = ref<AuditWithdrawReqVO>({
  id: 0,
  auditResult: WithdrawStatusEnum.APPROVED,
  auditRemark: '',
  expectedTime: '',
  priority: PriorityEnum.NORMAL
})

// 计算属性
const dialogTitle = computed(() => {
  return auditType.value === 'approve'
    ? t('settle.withdraw.auditApprove')
    : t('settle.withdraw.auditReject')
})

// 选项数据
const priorityOptions = {
  [PriorityEnum.LOW]: t('settle.withdraw.priorities.low'),
  [PriorityEnum.NORMAL]: t('settle.withdraw.priorities.normal'),
  [PriorityEnum.HIGH]: t('settle.withdraw.priorities.high'),
  [PriorityEnum.URGENT]: t('settle.withdraw.priorities.urgent')
}

// 表单校验规则
const formRules = reactive<FormRules>({
  auditRemark: [
    {
      required: true,
      message: t('settle.common.pleaseInput') + t('settle.withdraw.auditRemark'),
      trigger: 'blur'
    }
  ],
  expectedTime: [
    {
      required: true,
      message: t('settle.withdraw.rules.expectedTimeRequired'),
      trigger: 'change'
    }
  ],
  priority: [
    {
      required: true,
      message: t('settle.withdraw.rules.priorityRequired'),
      trigger: 'change'
    }
  ]
})

// 事件定义
const emit = defineEmits(['success'])

/**
 * 打开对话框
 */
const open = (withdraw: WithdrawVO, type: 'approve' | 'reject') => {
  dialogVisible.value = true
  auditType.value = type
  withdrawData.value = withdraw
  resetForm()
  formData.value.id = withdraw.id
  formData.value.auditResult =
    type === 'approve' ? WithdrawStatusEnum.APPROVED : WithdrawStatusEnum.REJECTED
}

/**
 * 提交审核
 */
const submitForm = async () => {
  const valid = await formRef.value?.validate()
  if (!valid) return

  loading.value = true
  try {
    await auditWithdrawApplyApi(formData.value)
    ElMessage.success(t('settle.withdraw.tips.auditSuccess'))
    dialogVisible.value = false
    emit('success')
  } catch (error) {
    console.error('审核提现失败:', error)
  } finally {
    loading.value = false
  }
}

/**
 * 重置表单
 */
const resetForm = () => {
  formData.value = {
    id: 0,
    auditResult: WithdrawStatusEnum.APPROVED,
    auditRemark: '',
    expectedTime: '',
    priority: PriorityEnum.NORMAL
  }
  formRef.value?.resetFields()
}

/**
 * 禁用日期
 */
const disabledDate = (time: Date) => {
  // 禁用过去的日期
  return time.getTime() < Date.now() - 24 * 60 * 60 * 1000
}

// 暴露方法
defineExpose({ open })
</script>

<style scoped>
.mb-20px {
  margin-bottom: 20px;
}

.amount-text {
  color: #409eff;
  font-weight: bold;
  font-size: 16px;
}

.fee-text {
  color: #e6a23c;
  font-weight: bold;
}

.actual-text {
  color: #67c23a;
  font-weight: bold;
  font-size: 16px;
}

.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}
</style>
